package com.zw.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.xmlbeans.impl.xb.xsdschema.impl.PublicImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zw.entity.Commodity;
import com.zw.entity.Order;
import com.zw.entity.Shopping;
import com.zw.entity.User;
import com.zw.service.CommodityService;
import com.zw.service.OrderService;
import com.zw.service.ShoppingService;
import com.zw.service.UserService;

import net.sf.json.JSONObject;

@Controller
public class OrderController{

	@Autowired 
	private OrderService orderService;

	@Autowired
	private ShoppingService shoppingService;
	@Autowired
	private CommodityService commodityService;

	@Autowired
	private KafkaTemplate<String, Object> template;

	@RequestMapping("/insertOrder")
	@ResponseBody
	public String insertOrder(Commodity c,HttpServletRequest request) throws Exception {
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");
		Order order=setValue(c, user);
		JSONObject object=JSONObject.fromObject(order);
		String string=object.toString();
		template.send("insert_order","insert",string);
		return "success";
	}

	/***
	 * 为对象赋值
	 * @param c 商品对象
	 * @param user 用户对象
	 * @return
	 */
	private Order setValue(Commodity c,User user){
		Order order=new Order();
		order.setCommoditycount(c.getCommoditycount());
		order.setCommodityid(c.getCommodityid());
		order.setUserid(user.getUserid());
		return order;
	}

	@KafkaListener(id="insert",topics = "insert_order")
	public void listen(ConsumerRecord<?, ?> cr) throws Exception {
		String string=(String) cr.value();
		JSONObject object=JSONObject.fromObject(string);
		Order order=(Order) JSONObject.toBean(object, Order.class);
		orderService.insertOrder(order);
	}

	@RequestMapping("/purchaseCommodity")
	@ResponseBody
	public String purchaseCommodity(@RequestParam(value = "arr[]")  int[]  arr){

		for (int i = 0; i < arr.length; i++) {
			Shopping shopping=shoppingService.selectShoppingBySid(arr[i]);
			Order order=new Order();
			order.setCommoditycount(shopping.getNum());
			order.setCommodityid(shopping.getCommodityid());
			order.setUserid(shopping.getUserid());
			JSONObject object=JSONObject.fromObject(order);
			String string=object.toString();
			template.send("insert_order","insert",string);
			shoppingService.deleteShopping(arr[i]);
		}
		return "success";
	}

	@RequestMapping("/selectOrder")
	public ModelAndView selectOrder(Integer pageNum,Integer maxPage,HttpSession session){
		if(pageNum<=0){
			pageNum=1;
		}
		if(maxPage!=null && pageNum>maxPage){
			pageNum=maxPage;
		}
		User user=(User) session.getAttribute("user");
		PageHelper.offsetPage(pageNum, 5);
		List<Order> list=orderService.selectOrderAll(user.getUserid());
		list.stream().forEach(order ->{
			order.setCommodity(commodityService.select(order.getCommodityid()));
		});
		PageInfo<Order> pageInfo=new PageInfo<>(list);
		maxPage=pageInfo.getPages();
		ModelAndView modelAndView=new ModelAndView("/Order");
		modelAndView.addObject("orderList", list);
		modelAndView.addObject("pageNum", pageNum);
		modelAndView.addObject("maxPage", maxPage);
		return modelAndView;
	}
	
	
	@RequestMapping("/confirm")
	@ResponseBody
	public String confirm(@RequestParam(value = "arr[]")  int[]  arr){
		try {
			for (int i = 0; i < arr.length; i++) {
				Order order=orderService.selectOrder(arr[i]);
				order.setOrderstate(2);
				orderService.updateOrder(order);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "success";
	}
}
